﻿using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace HeritageAcademy.Library.Controls
{
	/// <summary>
	/// Message control class.
	/// </summary>
	public partial class ErrorMessage
    {
        #region Parameters
        // Local value of message type
        private HeritageAcademy.Shared.Message.Type messageType;
        #endregion

        #region Ctor
        public ErrorMessage()
		{
			InitializeComponent();
            MouseLeftButtonDown += ErrorMessage_MouseLeftButtonDown;
		}
        #endregion

        #region Handlers
        /// <summary>
        /// Message control mouse click event handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Mouse button event arguments</param>
        void ErrorMessage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var close = FindResource("CloseMessage") as Storyboard;
            if (close != null) close.Begin(this);
        }
	    #endregion

        #region Properties
        /// <summary>
        /// Message type.
        /// </summary>
        public HeritageAcademy.Shared.Message.Type MessageType
        {
            get { return messageType; }
            set 
            { 
                messageType = value;
                UpdateDetails();
            }
        }

        /// <summary>
        /// Message text.
        /// </summary>
        public string Message
        {
            get { return tbMessage.Text; }
            set { tbMessage.Text = value; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Show message with manual close button.
        /// </summary>
        /// <param name="message">Message text string</param>
        /// <param name="type">Message type</param>
        public void Show(string message, HeritageAcademy.Shared.Message.Type type)
        {
            Show(message, type , false);
        }

        /// <summary>
        /// Show message with autoclose event.
        /// </summary>
        /// <param name="message">Message text string</param>
        /// <param name="type">Message type</param>
        /// <param name="isAutoHide">True if you need autoclose</param>
        public void Show(string message, HeritageAcademy.Shared.Message.Type type, bool isAutoHide)
        {
            // set values
            messageType = type;
            tbMessage.Text = message.ToUpper();
            UpdateDetails();

            // play animation
            var show = FindResource("ShowMessage") as Storyboard;
            if (isAutoHide)
            {
                if (show != null)
                {
                    show = show.Clone();
                    show.Completed += show_Completed;
                }
            }

            if (show != null) show.Begin(this);
        }

        /// <summary>
        /// Auto close message animation handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">System event arguments</param>
        void show_Completed(object sender, System.EventArgs e)
        {
            // unhandle show.completed animation
            var show = FindResource("ShowMessage") as Storyboard;
            if (show != null)
            {
                show = show.Clone();
                show.Completed -= show_Completed;
            }

            // begin close animation
            var autoClose = FindResource("AutoCloseMessage") as Storyboard;
            if (autoClose != null) autoClose.Begin(this);
        }

        /// <summary>
        /// Update visuals with new values.
        /// </summary>
        private void UpdateDetails()
        {
            switch (messageType)
            {
                case HeritageAcademy.Shared.Message.Type.Error:
                        recMarker.Fill = new SolidColorBrush(Colors.Red);
                        tbMessageType.Text = "ОШИБКА";
                        break;
                case HeritageAcademy.Shared.Message.Type.Warrning:
                        recMarker.Fill = new SolidColorBrush(Colors.Yellow);
                        tbMessageType.Text = "ВНИМАНИЕ";
                        break;
                case HeritageAcademy.Shared.Message.Type.Info:
                        recMarker.Fill = new SolidColorBrush(Colors.Green);
                        tbMessageType.Text = "ИНФОРМАЦИЯ";
                        break;
            }
        }
        #endregion
    }
}